// 公共的请求方法
export async function getTextTest(text, n) {
    try {
        const url = '/garbage/textTesting';
        const { data: res } = await this.$http[0].post(
            url,
            this.$objTostring(
                this.$removeField({
                    text
                })
            ), {
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            }
        );
        console.log(res);
        if (res.code != '0000') {
            throw new Error(res.message);
        }
        if (res.data[0].suggestion == 'pass') {
            this.legals ? (this.legals[n] = false, this.legals = [...this.legals]) : ''


        } else {
            this.legals ? this.legals[n] = true : ''
            throw new Error(res.data[0].testStatus);
        }
    } catch (err) {
        console.dir(err);
        this.$message.error({
            message: err.message,
            duration: 1500
        });
    }
}

export async function getImgTest(e, target, file, n) {
    try {
        var formdata = new FormData();
        formdata.append('image', file);
        const url = '/garbage/imgTesting';
        const { data: res } = await this.$http[0].post(
            url, formdata, {
                headers: {
                    'Content-Type': 'multipart/form-data'
                }
            }
        );
        if (res.code != '0000') {
            this.$nextTick(() => {
                console.log(this.$refs['img'])
                this.show = true;
                this.$refs['img'].src = this.$getObjectURL(file)
            })
            throw new Error(res.message);
        }
        if (res.data && res.data.length > 0) {
            if (res.data.some(item => { return item.suggestion !== 'pass'; })) {
                this.legals ? this.legals[n] = true : '';
                var item_ = res.data.find(item_ => {
                    return item_.suggestion !== 'pass';
                })
                throw new Error(item_.testStatus);
            } else {
                this.legals ? (this.legals[n] = false, this.legals = [...this.legals]) : ''
                    // 图片合法时才上传
                this.uploadFile.call(this, e, target, file);
            }
        } else {
            res.data = [];
        }
    } catch (err) {
        this.$nextTick(() => {
            console.log(this.$refs['img'])
            this.show = true;
            this.$refs['img'].src = this.$getObjectURL(file)
        })

        console.dir(err);
        this.$message.error({
            message: err.message,
            duration: 1500
        });
    }
}


export async function getImgTest_plus(e, target, file, n, callback, avatar) {
    try {
        this.$nextTick(() => {
            this.avatar = this.$getObjectURL(file);
            // if (avatar) {
            //     // 给传进来参数赋值
            //     this.avatar = this.$getObjectURL(file)
            // } else {
            //     // if ('src' in this.query) {
            //     //     // this.query.src = this.$getObjectURL(file)
            //     //     // 目前只有editmenu_1.vue 特殊
            //     //     // this.SET_EDITMENU_QUERY_1({ ...this.query, src: this.$getObjectURL(file) })
            //     // } else {
            //     //     this.show = true
            //     //     console.log(this.$refs['img'])
            //     //     this.$refs['img'].src = this.$getObjectURL(file)
            //     // }

            //     this.show = true
            //     console.log(this.$refs['img'])
            //     this.$refs['img'].src = this.$getObjectURL(file)
            // }

        })
        var formdata = new FormData();
        formdata.append('image', file);
        const url = '/garbage/imgTesting';
        const { data: res } = await this.$http[0].post(
            url, formdata, {
                headers: {
                    'Content-Type': 'multipart/form-data'
                }
            }
        );
        if (res.code != '0000') {
            this.$nextTick(() => {
                console.log(this.$refs['img'])
                this.show = true;
                this.$refs['img'].src = this.$getObjectURL(file)
            })
            throw new Error(res.message);
        }
        if (res.data && res.data.length > 0) {
            if (res.data.some(item => { return item.suggestion !== 'pass'; })) {
                this.legals ? this.legals[n] = true : '';
                var item_ = res.data.find(item_ => {
                    return item_.suggestion !== 'pass';
                })
                throw new Error(item_.testStatus);
            } else {
                this.legals ? (this.legals[n] = false, this.legals = [...this.legals]) : ''
                    // 图片合法时才上传
                    // this.uploadFile.call(this, e, target, file);
                callback && callback.call(this, e, target, file)
                this.coverImage = file;
            }
        } else {
            res.data = [];
        }

    } catch (err) {
        this.$nextTick(() => {
            this.show = true
            this.$refs['img'].src = this.$getObjectURL(file)
        })

        console.dir(err);
        this.$message.error({
            message: err.message,
            duration: 1500
        });
    }
}



// 锁住
export async function handleLock(index, row, callback, url, id) {
    try {
        // const url = '/song/lock';
        const { data: res } = await this.$http[0].get(url, {
            headers: {
                name: localStorage.getItem('ms_username')
            },
            params: {
                // id: row.id
                id
            }
        });
        if (res.code != '0000') {
            throw new Error(res.message);
        }
        callback && callback(index, row);
    } catch (err) {
        console.dir(err);
        this.$message.error({
            message: err.message,
            duration: 1500
        });
    }
}
// 解锁
export async function handleUnlock(callback, url, id) {
    try {
        // const url = '/song/unlock';
        const { data: res } = await this.$http[0].get(url, {
            params: {
                // id: this.form.id
                id
            }
        });
        if (res.code != '0000') {
            throw new Error(res.message);
        }
        callback && callback();
    } catch (err) {
        console.dir(err);
        this.$message.error({
            message: err.message,
            duration: 1500
        });
    }
}

export async function getSidebar() {
    try {
        const url = '/user/menus';
        const { data: res } = await this.$http[4].get(url, {
            params: this.$removeField({
                serviceId: localStorage.getItem('code')
            })
        });
        console.log('侧边栏', res);
        if (res.code != '0000') {
            throw new Error(res.message);
        }
        // this.sidebarData = res.data;
        this.SET_SLIDEBAR_DATA(res.data);
        console.log('sss:', this.sidebarData);
    } catch (err) {
        console.dir(err);
        this.SET_SLIDEBAR_DATA([])
        this.$message.error({
            message: err.data.message,
            duration: 1500
        });
    }
}



// export async function getSidebar1() {
//     try {
//         const url = '/user/menus';
//         const { data: res } = await this.$http[4].get(url, {
//             params: this.$removeField({
//                 serviceId: localStorage.getItem('code')
//             })
//         });
//         console.log('侧边栏', res);
//         if (res.code != '0000') {
//             throw new Error(res.message);
//         }
//         return res.data || []
//             // this.sidebarData = res.data;
//             // this.SET_SLIDEBAR_DATA(res.data);
//             // console.log('sss:', this.sidebarData);
//     } catch (err) {
//         console.dir(err);
//         // this.SET_SLIDEBAR_DATA([])
//         this.$message.error({
//             message: err.data.message,
//             duration: 1500
//         });
//     }
// }